/* Import the BLS capital and capital-related measures for NAICS3=327
   and merge them into one file.
   Convert indexes and real variables to 1997 and 2002 bases. 
   Construct capital rental price.
   

   Output: BLS_kapital_327.sas7bdat
*/


%include "ASMimplibs.sas";

/* Capital income: */
PROC IMPORT OUT= WORK.ky_327 
 DATAFILE= "ky_327.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

/* Productive Capital Stock (Billions of 2005 Dollars) */
PROC IMPORT OUT= WORK.pk_327 
 DATAFILE= "pk_327.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

/* Capital Composition (Ratio of Capital Input to Productive Stock) */
PROC IMPORT OUT= WORK.kc_327 
 DATAFILE= "kc_327.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

/* Depreciation Rate of Wealth Stock */
PROC IMPORT OUT= WORK.kdpr_327 
 DATAFILE= "kdpr_327.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

/* [Investment] Price Deflators */
PROC IMPORT OUT= WORK.piinv_327 
 DATAFILE= "piinv_327.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

data ky_327; set ky_327 (drop=All_Assets); NAICS3='327'; run;
data kdpr_327; set kdpr_327; NAICS3='327'; run;
data pk_327; set pk_327 (drop=All_Assets); NAICS3='327';run;
data kc_327; set kc_327 (drop=All_Assets); NAICS3='327'; run;
data piinv_327; set piinv_327 (drop=All_Assets); NAICS3='327'; run;

/* Change base year of productive capital stock, capital composition, and price deflators to 1997. */
/* Also create a version with base year=2002. */ 

data kc_327_1997 (keep=equipment structures); set kc_327; if year=1997; run;
data piinv_327_1997 (keep=equipment structures); set piinv_327; if year=1997; run;
data kc_327_2002 (keep=equipment structures); set kc_327; if year=2002; run;
data piinv_327_2002 (keep=equipment structures); set piinv_327; if year=2002; run;

proc datasets library=work;
modify kc_327_1997;
 rename equipment=eqkc_base2005_in_1997;
 rename structures=stkc_base2005_in_1997;
modify piinv_327_1997;
 rename equipment=piinve_base2005_in_1997;
 rename structures=piinvs_base2005_in_1997;
modify kc_327_2002;
 rename equipment=eqkc_base2005_in_2002;
 rename structures=stkc_base2005_in_2002;
modify piinv_327_2002;
 rename equipment=piinve_base2005_in_2002;
 rename structures=piinvs_base2005_in_2002;
run;

data kcpiinv_327_9702;
 merge kc_327_1997 piinv_327_1997 kc_327_2002 piinv_327_2002;
run;

proc datasets library=work;
modify ky_327;
 rename equipment=eqky;
 rename structures=stky;
modify pk_327;
 rename equipment=eqpk;
 rename structures=stpk;
modify kc_327;
 rename equipment=eqkc;
 rename structures=stkc;
modify kdpr_327;
 rename All_Assets=kdpr;
 rename equipment=eqdpr;
 rename structures=stdpr;
modify piinv_327;
 rename equipment=piinve;
 rename structures=piinvs;
run;

data kapital_327;
 merge ky_327 pk_327 kc_327 kdpr_327 piinv_327;
 by year;
run;

data kapital_327;
 if _N_=1 then set kcpiinv_327_9702; 
 set kapital_327;
 eqpk_base97 = eqpk*piinve_base2005_in_1997; 
 stpk_base97 = stpk*piinvs_base2005_in_1997; 
 eqpk_base02 = eqpk*piinve_base2005_in_2002; 
 stpk_base02 = stpk*piinvs_base2005_in_2002; 

 eqkc_base97 = 100*eqkc/eqkc_base2005_in_1997; 
 stkc_base97 = 100*stkc/stkc_base2005_in_1997; 
 eqkc_base02 = 100*eqkc/eqkc_base2005_in_2002; 
 stkc_base02 = 100*stkc/stkc_base2005_in_2002;

 piinve_base97 = piinve/piinve_base2005_in_1997;
 piinvs_base97 = piinvs/piinvs_base2005_in_1997;
 piinve_base02 = piinve/piinve_base2005_in_2002;
 piinvs_base02 = piinvs/piinvs_base2005_in_2002;

 eqrkl_base97 = eqky/(eqpk_base97*eqkc_base97/100);
 strkl_base97 = stky/(stpk_base97*stkc_base97/100);
 eqrkl_base02 = eqky/(eqpk_base02*eqkc_base02/100);
 strkl_base02 = stky/(stpk_base02*stkc_base02/100);

run;


proc datasets library=work;
modify kapital_327;

 label eqrkl_base97 = 'Rental Price of Capital in Levels, Equipment, NAICS3, base year=1997';
 label strkl_base97 = 'Rental Price of Capital in Levels, Structures, NAICS3, base year=1997';
 label eqrkl_base02 = 'Rental Price of Capital in Levels, Equipment, NAICS3, base year=2002';
 label strkl_base02 = 'Rental Price of Capital in Levels, Structures, NAICS3, base year=2002';

 label eqky='Equipment Capital Income (Billions of nominal Dollars), NAICS3';
 label stky='Structures Capital Income (Billions of nominal Dollars), NAICS3';
 label eqpk_base97 = 'Equipment Productive Capital Stock (Billions of 1997 Dollars), NAICS3';
 label stpk_base97 = 'Structures Productive Capital Stock (Billions of 1997 Dollars), NAICS3';
 label eqpk_base02 = 'Equipment Productive Capital Stock (Billions of 2002 Dollars), NAICS3';
 label stpk_base02 = 'Structures Productive Capital Stock (Billions of 2002 Dollars), NAICS3';

 label eqkc_base97 = 'Equipment Capital Composition (Ratio of Capital Input to Productive Stock), Base year=1997, NAICS3';
 label stkc_base97 = 'Structures Capital Composition (Ratio of Capital Input to Productive Stock), Base year=1997, NAICS3';
 label eqkc_base02 = 'Equipment Capital Composition (Ratio of Capital Input to Productive Stock), Base year=2002, NAICS3';
 label stkc_base02 = 'Structures Capital Composition (Ratio of Capital Input to Productive Stock), Base year=2002, NAICS3';

 label kdpr = 'Depreciation Rate for All Assets, NAICS3';
 label eqdpr = 'Equipment Depreciation Rate, NAICS3';
 label stdpr = 'Structures Depreciation Rate, NAICS3';
 label piinve_base97 = 'Equipment Investment Price Deflator, NAICS3, base year=1997';
 label piinvs_base97 = 'Structures Investment Price Deflator, NAICS3, base year=1997';
 label piinve_base02 = 'Equipment Investment Price Deflator, NAICS3, base year=2002';
 label piinvs_base02 = 'Structures Investment Price Deflator, NAICS3, base year=2002';
run;

data bls.BLS_capital_327 (keep = year eqrkl_base97 strkl_base97 eqrkl_base02 strkl_base02 eqky stky eqpk_base97 stpk_base97
 eqpk_base02 stpk_base02 eqkc_base97 stkc_base97 eqkc_base02 stkc_base02 kdpr eqdpr stdpr piinve_base97 piinvs_base97 piinve_base02 piinvs_base02);
 set kapital_327;
run;

proc contents data=bls.BLS_capital_327;
title1 "BLS capital variables";
run;

proc print data=bls.BLS_capital_327;
title1 "BLS capital variables, 1987-2010";
run;
